package com.maila.simplerecord.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.maila.simplerecord.R;
import com.maila.simplerecord.utils.ImgBrower;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/2.
 */

public class ImgAdapter extends BaseAdapter {
    Context context;
    List<String> imgs;

    public ImgAdapter(Context context) {
        this.context = context;
        imgs = new ArrayList<>();
    }

    public void setImgs(List<String> imgs) {
        this.imgs = imgs;
        notifyDataSetChanged();
    }

    public void remove(int position) {
        this.imgs.remove(position);
        notifyDataSetChanged();
    }
    public List<String> getImgs() {
        return imgs;
    }

    public void addallImgs(List<String> imgs) {
        this.imgs.addAll(imgs);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return imgs.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_subd_imge, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide.with(context).load(Uri.parse("file://" + imgs.get(position))).error(R.drawable.recodedemo).into(holder.ivProject);
        holder.ivProject.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImgBrower.getImageBrower(context,position,imgs,"",v);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.iv_project)
        ImageView ivProject;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
